
//
//  WIFIManagerHeaderView.m
//  Router
//
//  Created by teki on 15/10/16.
//  Copyright © 2015年 dc. All rights reserved.
//



#define upLoadLabelFont  ([UIFont systemFontOfSize:27.0f])
#define kbFont  ([UIFont systemFontOfSize:15.0f])


//typedef <#returnType#>(^<#name#>)(<#arguments#>);
#import "WIFIManagerHeaderView.h"
#import "WIFIModel.h"
#import "RateModel.h"

@interface WIFIManagerHeaderView ()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *uploadLabel;
@property (nonatomic, strong) UILabel *downloadLabel;

@property (nonatomic, copy) ButtonClick editBlock;
@property (nonatomic, copy) ButtonClick refreshBlock;
@end


@implementation WIFIManagerHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}


- (instancetype)init
{
    self = [super init];
    if (self)
    {
        [self initUI];
    }
    return self;
}


- (void)initUI
{
    self.backgroundColor  = [UIColor colorWithHexString:@"#e1f6de"];
    UIColor * textColor = [UIColor colorWithHexString:@"#5fa656"];
    UIFont *nameLabelFont = [UIFont systemFontOfSize:16.0f];
    
    UIImageView *imgView = [UIImageView new];
    imgView.translatesAutoresizingMaskIntoConstraints = NO;
    UIImage *image = [UIImage imageNamed:@"home_wifi"];
    imgView.image = image;
    [self addSubview:imgView];
    [imgView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.top).offset(20);
        make.left.equalTo(self.left).offset(18);
        make.size.equalTo(image.size);
    }];
    
    
    self.nameLabel = [self addLabelWithText:@"LightNest_0000002" font:nameLabelFont color:textColor];
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.right).offset(8);
        make.centerY.equalTo(imgView);
    }];
    
    
    UIImage *refreshNormalImage = [UIImage imageNamed:@"home_grade_pre"];
    UIImage *refreshSelectedImage = [UIImage imageNamed:@"home_grade"];
    UIButton *refreshBtn = [self addButtonWithNormalImage:refreshNormalImage selectedImage:refreshSelectedImage target:self action:@selector(refreshButtonPressed:)];
    [refreshBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-18);
        make.top.equalTo(self).offset(13);
        make.size.equalTo(refreshNormalImage.size);
    }];
    
    
    UIImage *editNormalImage = [UIImage imageNamed:@"home_wifi_edit_pre"];
    UIImage *editSelectedImage = [UIImage imageNamed:@"home_wifi_edit"];
    UIButton *editBtn = [self addButtonWithNormalImage:editNormalImage selectedImage:editSelectedImage target:self action:@selector(editButtonPressed:)];
    [editBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(refreshBtn.left).offset(-7);
        make.centerY.equalTo(refreshBtn);
        make.size.equalTo(editNormalImage.size);
    }];
    
    
    UILabel *lineLabel = [self addLabelWithText:@"" font:nil color:nil];
    lineLabel.backgroundColor = [UIColor colorWithHexString:@"#a2a1a2"];
    [lineLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(editBtn.bottom).offset(15);
        make.height.equalTo(1);
        make.left.equalTo(18);
        make.right.equalTo(-18);
    }];
    
    UIView *contentView =[UIView new];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:contentView];
    [contentView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineLabel.bottom).offset(23);
        make.centerX.equalTo(self);
        make.bottom.equalTo(-23);
    }];
    
    UIImageView *upLoadImgView = [UIImageView new];
    upLoadImgView.translatesAutoresizingMaskIntoConstraints = NO;
    UIImage *upImage = [UIImage imageNamed:@"device_upload"];
    upLoadImgView.image = upImage;
    [contentView addSubview:upLoadImgView];
    [upLoadImgView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.centerY.equalTo(contentView);
        make.size.equalTo(image.size);
    }];
    
    
    self.uploadLabel = [contentView addLabelWithText:@"" font:nameLabelFont color:textColor];
    [self.uploadLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(upLoadImgView.right).offset(8);
        make.centerY.equalTo(contentView);
    }];
    
    
    
    UIImageView *downLoadImgView = [UIImageView new];
    downLoadImgView.translatesAutoresizingMaskIntoConstraints = NO;
    UIImage * downImage = [UIImage imageNamed:@"device_download"];
    downLoadImgView.image = downImage;
    [contentView addSubview:downLoadImgView];
    [downLoadImgView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.uploadLabel.right).offset(42);
        make.centerY.equalTo(contentView);
        make.size.equalTo(image.size);
    }];
    
    self.downloadLabel = [contentView addLabelWithText:@"" font:nameLabelFont color:textColor];
    [self.downloadLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(0);
        make.centerY.equalTo(contentView);
        make.left.equalTo(downLoadImgView.right).offset(8);
    }];  
}


- (void)setDeviceName:(NSString *)deviceName
{
    _deviceName = deviceName;
    self.nameLabel.text = deviceName;
}


- (void)setUpLoadSpeed:(NSString *)upLoadSpeed
{
    _upLoadSpeed = upLoadSpeed;
    NSMutableAttributedString *string = [self attributeStringWithString:upLoadSpeed];
    [self.uploadLabel setAttributedText:string];
}



- (void)setDownLoadSpeed:(NSString *)downLoadSpeed
{
    _downLoadSpeed = downLoadSpeed;
    NSMutableAttributedString *string = [self attributeStringWithString:downLoadSpeed];
    [self.downloadLabel setAttributedText:string];
}


- (void)setWifiModel:(WIFIModel *)wifiModel
{
    _wifiModel = wifiModel;
    if (wifiModel)
    {
        [self setDeviceName:wifiModel.ssid];
    }
    
}



- (void)setRateModel:(RateModel *)rateModel{
    _rateModel = rateModel;
    if (rateModel) {
        self.upLoadSpeed = [self formatSpeed:rateModel.up];
        self.downLoadSpeed = [self formatSpeed:rateModel.down];
    }
    else{
        [self setUpLoadSpeed:@"0KB"];
        [self setDownLoadSpeed:@"0KB"];
    }

}

- (NSString *)formatSpeed:(NSString *)speed {
    int value = [speed intValue];
    if (value > 1024*1024) {
        return [[NSString alloc] initWithFormat:@"%.01fMB", value/(1024*1024.0)];
    } else {
        return [[NSString alloc] initWithFormat:@"%.01fKB", value/1024.0];
    }
}




- (NSMutableAttributedString *)attributeStringWithString:(NSString *)string
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:string];
//    NSRange range=[string rangeOfString:@"KB"];
    [attributeString addAttribute:NSFontAttributeName value:kbFont range:NSMakeRange(string.length - 2, 2)];
    [attributeString addAttribute:NSFontAttributeName value:upLoadLabelFont range:NSMakeRange(0, string.length - 2)];
    return attributeString;
}






- (void)setEditButtonClick:(ButtonClick)editBlock
{
    _editBlock = editBlock;
}


- (void)setRefreshButtonClick:(ButtonClick)refreshBlock
{
    _refreshBlock = refreshBlock;
}



#pragma mark -
#pragma mark   IBActions
- (void)refreshButtonPressed:(UIButton *)sender
{
    if (self.refreshBlock)
    {
        self.refreshBlock();
    }

}


- (void)editButtonPressed:(UIButton *)sender
{
    if (self.editBlock) {
        self.editBlock();
    }
}



@end
